package com.ruoyi.jinchuguanli.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jinchuguanli.domain.Gouhuoruku;
import com.ruoyi.jinchuguanli.domain.GouhuorukuReturn;
import com.ruoyi.jinchuguanli.domain.Xiaoshouchuku;
import com.ruoyi.jinchuguanli.domain.XiaoshouchukuReturn;
import com.ruoyi.jinchuguanli.service.IGouhuorukuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * rukuController
 *
 * @author ruoyi
 * @date 2023-11-08
 */
@Controller
@RequestMapping("/jinchuguanli/ruku")
public class GouhuorukuController extends BaseController {
    private final String prefix = "jinchuguanli/ruku";

    @Autowired
    private IGouhuorukuService gouhuorukuService;

    @RequiresPermissions("jinchuguanli:ruku:view")
    @GetMapping()
    public String ruku () {
        return prefix + "/ruku";
    }

    /**
     * 查询ruku列表
     */
    @RequiresPermissions("jinchuguanli:ruku:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list (Gouhuoruku gouhuoruku,Integer pageNum,Integer pageSize) {
        startPage();
//            List<Gouhuoruku> list = gouhuorukuService.selectGouhuorukuList(gouhuoruku);
//        List<GouhuorukuReturn> list = gouhuorukuService.selectGouhuorukuReturnList(gouhuoruku);
//        return getDataTable(list);

        List<GouhuorukuReturn> list = new ArrayList<>();
        PageInfo<Gouhuoruku> gouhuorukuPageInfo =
                PageUtils.startPage(pageNum,pageSize).doSelectPageInfo(()->
                        gouhuorukuService.selectGouhuorukuList(gouhuoruku));
        PageInfo<GouhuorukuReturn> gouhuorukuReturnPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(gouhuorukuPageInfo,gouhuorukuReturnPageInfo);

        List<Gouhuoruku> xiaoshouchukuList = gouhuorukuPageInfo.getList();
        for (Gouhuoruku gr:xiaoshouchukuList
        ) {
            GouhuorukuReturn xiaoshouchukuReturn = gouhuorukuService.aReturnFormat(gr);
            list.add(xiaoshouchukuReturn);
        }
        gouhuorukuReturnPageInfo.setList(list);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(gouhuorukuReturnPageInfo.getTotal());

        return rspData;
    }

    /**
     * 导出ruku列表
     */
    @RequiresPermissions("jinchuguanli:ruku:export")
    @Log(title = "ruku", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export (Gouhuoruku gouhuoruku) {
        List<Gouhuoruku> list = gouhuorukuService.selectGouhuorukuList(gouhuoruku);
        ExcelUtil<Gouhuoruku> util = new ExcelUtil<Gouhuoruku>(Gouhuoruku.class);
        return util.exportExcel(list, "ruku数据");
    }

    /**
     * 新增ruku
     */
    @GetMapping("/add")
    public String add () {
        return prefix + "/add";
    }

    /**
     * 新增保存ruku
     */
    @RequiresPermissions("jinchuguanli:ruku:add")
    @Log(title = "ruku", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave (Gouhuoruku gouhuoruku) {
        return toAjax(gouhuorukuService.insertGouhuoruku(gouhuoruku));
    }

    /**
     * 修改ruku
     */
    @RequiresPermissions("jinchuguanli:ruku:edit")
    @GetMapping("/edit/{id}")
    public String edit (@PathVariable("id") Integer id, ModelMap mmap) {
        Gouhuoruku gouhuoruku = gouhuorukuService.selectGouhuorukuById(id);
        mmap.put("gouhuoruku", gouhuoruku);
        return prefix + "/edit";
    }

    /**
     * 修改保存ruku
     */
    @RequiresPermissions("jinchuguanli:ruku:edit")
    @Log(title = "ruku", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave (Gouhuoruku gouhuoruku) {
        return toAjax(gouhuorukuService.updateGouhuoruku(gouhuoruku));
    }

    /**
     * 删除ruku
     */
    @RequiresPermissions("jinchuguanli:ruku:remove")
    @Log(title = "ruku", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove (String ids) {
        return toAjax(gouhuorukuService.deleteGouhuorukuByIds(ids));
    }
}
